//
//  BubbleView.m
//  VideoPlayer
//
//  Created by 白璐 on 16/9/18.
//  Copyright © 2016年 baidu. All rights reserved.
//

#import "BubbleView.h"

@implementation BubbleView

- (instancetype)initWithCoder:(NSCoder *)aDecoder {
    self = [super initWithCoder:aDecoder];
    
    self.layer.shadowColor = [UIColor blackColor].CGColor;
    self.layer.shadowOpacity = 1.0;
    self.layer.shadowRadius = 3.0;
    self.layer.shadowOffset = CGSizeMake(0.0f, 0.0f);
    
    return self;
}

- (IBAction)addVideo:(id)sender {
    if (self.addBlock) {
        self.addBlock();
    }
}

- (IBAction)openDownloadList:(id)sender {
    if (self.downloadListBlock) {
        self.downloadListBlock();
    }
}

- (void)drawRect:(CGRect)rect {
    CGContextRef context = UIGraphicsGetCurrentContext();
    
    CGContextSetLineWidth(context, 3.0f);
    CGContextSetFillColorWithColor(context, [UIColor whiteColor].CGColor);
    
    [self drawPath:context];
    
    CGContextFillPath(context);
}

- (void)drawPath:(CGContextRef)context {
    CGRect rect = self.bounds;
    CGFloat radius = 6.0f;
    CGFloat arrowHeight = 5.0f;
    
    CGFloat maxX = CGRectGetMaxX(rect) - 5;
    CGFloat minX = maxX - 105;
    CGFloat midX = maxX - 24;
    CGFloat minY = CGRectGetMinY(rect) + arrowHeight + 1.0f;
    CGFloat maxY = 100 + arrowHeight;
    
    CGContextMoveToPoint(context, midX - arrowHeight, minY);
    CGContextAddLineToPoint(context, midX, minY - arrowHeight);
    CGContextAddLineToPoint(context, midX + arrowHeight, minY);
    
    CGContextAddArcToPoint(context, maxX, minY, maxX, maxY, radius);
    CGContextAddArcToPoint(context, maxX, maxY, minX, maxY, radius);
    CGContextAddArcToPoint(context, minX, maxY, minX, minY, radius);
    CGContextAddArcToPoint(context, minX, minY, midX -arrowHeight, minY, radius);
    
    CGContextClosePath(context);
}

@end
